/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.freedoit.util;

import java.math.BigInteger;
import java.security.interfaces.RSAPublicKey;
import org.freedoit.common.util.FreeDoitDataUtils;

/**
 *
 * @author wanglei
 */
public class EncryptEngine {
    
    private static RSAPublicKey pubKey = null;
    public static void reset(String encryptedModulus, 
            String encryptedExponent, String password) {
        
        pubKey = FreeDoitDataUtils.generateRSAPublicKey(
                new BigInteger(FreeDoitDataUtils.decryptByAES(
                encryptedModulus, password)), 
                new BigInteger(FreeDoitDataUtils.decryptByAES(
                encryptedExponent, password)));
    }
    
    public static String doEncrypt(String plainText) {
        if (pubKey == null) {
            return null;
        }
        return FreeDoitDataUtils.encryptByRSA(pubKey, plainText);
    }
    
    public static String doDecrypt(String encryptedText) {
        if (pubKey == null) {
            return null;
        }
        return FreeDoitDataUtils.decryptByRSA(pubKey, encryptedText);
    }
    
}
